﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CommonControl;
using DevExpress.XtraTreeList.Nodes.Operations;
using DevExpress.XtraTreeList.Nodes;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmProductType : XtraForm
    {
        #region 初始化 加载数据
        public FrmProductType()
        {
            InitializeComponent();
            InitGrid();

        }

        private void FrmProductType_Load(object sender, EventArgs e)
        {
            LoadData();

            tlsMain.NodesIterator.DoOperation(new CommonControl.DxGridHelper.CollapseExceptSpecifiedOperation(tlsMain.FocusedNode));
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            var bll = new DMZS.BLL.ProductType();
            try
            {
                tlsMain.DataSource = bll.GetListForUIExpand(string.Empty).Tables[0];
            }
            catch (Exception ex)
            {
                Utils.WriteTxtLog(ex);
                XtraMessageBox.Show(Utils.GetRuntimeMessage(ex), Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            System.Data.DataTable dt = new System.Data.DataTable();
            //tlsMain.ExpandAll();
        }

        /// <summary>
        /// 初始网格显示
        /// </summary>
        private void InitGrid()
        {
            CommonControl.DxGridHelper.InitGridStyle(tlsMain);
        }
        #endregion


        #region 工具栏按钮事件
        //新增同级
        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var frm = new FrmProductType_Editor();
            frm.ID = 0;
            if (tlsMain.FocusedNode != null)
            {
                frm.ParentID = Convert.ToInt64(tlsMain.FocusedNode["ParentID"]);
            }
            else
            {
                frm.ParentID = 0;
            }
            frm.ShowDialog();
        }

        //新增子级
        private void btnNewChild_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var frm = new FrmProductType_Editor();
            frm.ID = 0;
            if (tlsMain.FocusedNode != null)
            {
                frm.ParentID = Convert.ToInt64(tlsMain.FocusedNode["ID"]);
            }
            else
            {
                frm.ParentID = 0;
            }
            frm.ShowDialog();
        }

        //刷新
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        //修改
        private void btnModify_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (tlsMain.FocusedNode != null)
            {
                var frm = new FrmProductType_Editor();
                frm.ID = Convert.ToInt64(tlsMain.FocusedNode["ID"]);
                frm.ShowDialog();
            }
        }

        //删除
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (tlsMain.FocusedNode == null) return;

            var text = string.Empty;
            if (tlsMain.FocusedNode.Nodes.Count != 0)
            {
                text = string.Format("确认删除,其下有{0}子项？\r注意：将同时删除子项！", tlsMain.FocusedNode.Nodes.Count);
            }
            else
            {
                text = "确认删除？";
            }

            if (DialogResult.No == XtraMessageBox.Show(text, Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                return;
            }

            var bll = new DMZS.BLL.ProductType();
            int result = bll.DeleteExpand(Convert.ToInt64(tlsMain.FocusedNode["ID"]));
            if (result > 0)
            {
                //XtraMessageBox.Show("删除成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
            else if (result == -1)
            {
                XtraMessageBox.Show("删除失败，建议刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                return;
            }
            else if (result == -2)
            {
                XtraMessageBox.Show("该类别或其子类别已被引用，无法删除。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                return;
            }

            LoadData();
        }
        #endregion


        #region 树网格事件
        //双击修改

        //树网格数字列
        private void tlsMain_CustomDrawNodeIndicator(object sender, DevExpress.XtraTreeList.CustomDrawNodeIndicatorEventArgs e)
        {
            var tmpTree = sender as DevExpress.XtraTreeList.TreeList;
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            var args = e.ObjectArgs as DevExpress.Utils.Drawing.IndicatorObjectInfoArgs;
            if (args != null)
            {
                var rowNum = tmpTree.GetVisibleIndexByNode(e.Node) + 1;
                args.DisplayText = rowNum.ToString();
            }
        }

        private void tlsMain_DoubleClick(object sender, EventArgs e)
        {
            if (tlsMain.FocusedNode.HasChildren) return;

            if (tlsMain.FocusedNode != null)
            {
                var frm = new FrmProductType_Editor();
                frm.ID = Convert.ToInt64(tlsMain.FocusedNode["ID"]);
                frm.ShowDialog();
            }
        }

        #endregion


    }
}
